//
//  NoteListViewController.m
//  MyNote
//
//  Created by Adiljan on 2016/11/16.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "NoteListViewController.h"
#import "AddViewController.h"
#import "AppDelegate.h"
#import "ContentItemCell.h"



#define APPNOte ((AppDelegate*)([UIApplication sharedApplication].delegate))
@interface NoteListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UILabel *lblContent;
@property (nonatomic, strong)NSArray *arrContent;
@end

@implementation NoteListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initViews];
    [self initNavigation];

    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    /*Adiljan 获取数据库数据*/
    [self getDataBase];
    [_tblList reloadData];
}
- (void)initNavigation{
    UIButton *btnAdd = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [btnAdd setImage:[UIImage imageNamed:@"add_image"] forState:UIControlStateNormal];
    [btnAdd addTarget:self action:@selector(btnAddClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:btnAdd];
    self.navigationItem.rightBarButtonItem = rightItem;
    UIButton *btnDelete = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [btnDelete setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [btnDelete addTarget:self action:@selector(btnDeleteClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btnDelete];
    self.navigationItem.leftBarButtonItem = leftItem;
}
-(void)initViews{
    self.view.backgroundColor = [UIColor blackColor];
    self.view.alpha = 0.7;
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBar.alpha = 0.7;
    self.title = @"备忘录列表";
    
    self.tblList.dataSource = self;
    self.tblList.delegate = self;
    [self.tblList registerNib:[UINib nibWithNibName:@"ContentItemCell" bundle:nil] forCellReuseIdentifier:@"ContentItemCell"];
//     [self.tblList registerClass:[ContentItemCell class] forCellReuseIdentifier:@"ContentItemCell"];
    self.tblList.tableFooterView = [[UIView alloc]init];
    
//    self.tblList.allowsMultipleSelectionDuringEditing = YES;
//    self.tblList.allowsSelectionDuringEditing = YES;
//    self.tblList.allowsSelection = YES;
//    self.tblList.allowsMultipleSelection = NO;
    
}
/*Adiljan 获取数据库数据*/
- (void)getDataBase{
//    NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData:APPNOte.noteModel.dataContent];
//    // 数组倒序排列
//    arr2 = (NSMutableArray *)[[arr2 reverseObjectEnumerator] allObjects];
//    self.arrContent = arr2;
}

/*Adiljan 跳转到添加页面*/
- (void)btnAddClick{
    AddViewController *add = [[AddViewController alloc]initWithNibName:@"AddViewController" bundle:nil];
    [self.navigationController pushViewController:add animated:YES];
    
}
/*Adiljan 删除*/
- (void)btnDeleteClick{
    //与当前相反！
//    [self setEditing:!self.editing animated:YES];
    [self startEditing];
}
//编辑按钮的点击
-(void) startEditing{
    self.tblList.editing = !self.tblList.editing;
    if (self.tblList.editing) {
        [self.tblList setEditing:YES animated:YES];
    }else{
        [self.tblList setEditing:NO animated:YES];
    }
    //支持同时选中多行
//    self.tblList.allowsMultipleSelectionDuringEditing = YES;
//    self.tblList.editing = !self.tblList.editing;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//是否可以编辑  默认的时YES
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//选择编辑的方式,按照选择的方式对表进行处理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
    }
    
}

//选择你要对表进行处理的方式  默认是删除方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

//选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
    
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    //    [self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
}


#pragma mark tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.arrContent.count!=0) {
        return self.arrContent.count;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        ContentItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentItemCell"];
        cell.lblContent.text = self.arrContent[indexPath.row][1];
        cell.lblTime.text = self.arrContent[indexPath.row][0];
    
 
    return cell;
    
}

// 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

@end
